Perl Data Types (Scalar) and Common Operators

Scalar Data

  • A scalar is the simplest kind of data that Perl manipulates
    • String of characters
    • primatives
  • Numbers
    • Perl always uses double-point precision for values (no difference between floats or integers)
    • Examples:
#Numbers
 $myint1 = 123456; 
 $myint2 = 123_456; # can use _ anywhere in number
 $mybig1 = 1.23e10; # e notation
 $mysmall1 = 0xff; # hex notation
 print "$myint1 \n$myint2 \n$mybig1 \n$mysmall1 \n"; 
 #Output:
 #123456
 #123456
 #12300000000
 #255
#Arithmetic
$ans = 2 + 3 # 5

$ans = 5.1 – 2.4 # 2.7

$ans = 3 * 12

$ans = 14 / 2

$ans = 7 % 3 # 1 (modulus)

$ans = 7 ** 3 # 343 (exponential, 7^3)
  • Strings
    • Sequence of characters from “” to “max memory” – Perl has no built-in limit for strings
    • Can come in two different types: ‘Single Quoted String’ vs “Double Quoted String”
    • ‘Single Quoted’
      • Any string within the quotes stand for itself (except the single quote character, which would terminate this string)
'hello\n' # hello + newline character
 'hello
 there' # hello + newline + there
 'don\'t forget' # don't forget # the \' is needed to continue this string to the end
    • “Double Quoted”
      • The backslash takes its full power definition, such as tab and newline characters
"hello world \n" # hello world + newline
 "coke\tsprite" # coke + tab + sprite
    • String Operators
      • Concatenation is . (period)
      • String repetition is done using the x character
"fred" x 3 # "fredfredfred"
 5 x 3.7 # "555" The "5" is converted to string and "3.7" is truncated to "3"
 "cat" x .7 # "" empty string result due to truncation
    • Generally Perl does all numbers / strings conversions by acknowledging the situation
      • Checks operators to determine what data types are expected
      • Does type conversions on the fly, some exceptions:
"Z" . 5*7 # "Z35" this results in Z concatenated to the result of 5*7
 "12fred" * " 3" # "36" the "fred" is truncated as the "*" operator requires number
  • Perl Warnings
    • When running perl programs, give the “-w” option to view warnings
$ perl -w my_program.pl
    • Or include the option in the header of the program file
#!/usr/bin/perl -w 
 use warnings; # two other ways to turn on warnings
  • Scalar Variables
    • A scalar variable holds a single scalar value, and is begins with $ in the variable name
    • Scalar variables in always preceded with $ (sigil)
    • Binary Assignment Operators
      • $myvar += 1; incremental
      • $str .= "\n"; concat at string end
    • Interpolation
      • Any variable within a string with double quotes is replaced with its value
print “fred at $n ${what}s. \n”;

print “fred at $n $what” . “s. \n”; #both these lines output the same
  • Operator Precedence – Order from highest to lowest:
    • Left to right
    • Parentheses
    • Auto incrementation/decrements (++/–)
    • Exponent **
    • Arithmetic
  • Comparison Operators
    • Have both characters and words to do the comparisons. Table:
Equal == eq
Not Equal != ne
Less than < lt
Greater than > gt
Less or equal <= le
Greater or equal >= ge
  • IF control
If ($name gt ‘fred') { print "here"; }
 Else { print "there"; }
  • IF control with Boolean values
$result = $name gt ‘fred';
 If ($result) {...}
    • For numbers, any value other than zero is true, zero is always false
    • For strings, empty string is false, anything else is true
    • Exception for strings # “0” is actually the save value as number zero so it is the only non-empty string that will result in false
  • Getting User Input
    • Easiest way is using <STDIN>
$line = <STDIN>;
 print "Input was: $line \n";
  • Chomp operator
    • Function with return value – returns the number of characters removed, returns zero if nothing is chomped
    • Removes all trailing newline characters
    • Can be called with or without parentheses
chomp ($text = <STDIN>);
 $result = chomp $variable;
  • The while Control Structure
$count = 0;
 while ($count < 10) { … $count += 1; … }
  • The undef Value
    • This is a scalar value all on its own, defaults to zero or empty string
$sum += 1; # sum = 1
 $string .= “hello world”; # string = hello world
  • The defined Function
    • Used to check undef variables
if(defined($variable)){
 print “True!”;
 } else { print “False!”; }